package com.qdxwx.data.service.picture;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.picture.PictureAlbumMapper;
import com.qdxwx.models.picture.PictureAlbum;
import com.qdxwx.models.picture.PictureAlbumVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class PictureAlbumServiceImpl implements PictureAlbumService {

    @Autowired
    private PictureAlbumMapper pictureAlbumMapper;
    @Autowired
    private PictureService pictureService;

    @Override
    public int insert(PictureAlbum pictureAlbum) {
        pictureAlbum.setUpdateTime(LocalDateTime.now());
        pictureAlbum.setCreateTime(LocalDateTime.now());
        return pictureAlbumMapper.insert(pictureAlbum);
    }

    @Override
    public PictureAlbum get(int id) {
        if (id < 1) return null;
        return pictureAlbumMapper.selectById(id);
    }

    @Override
    public List<PictureAlbum> getList(PictureAlbum pictureAlbum, PageRequest page) {
        return pictureAlbumMapper.selectList(pictureAlbum, page);
    }

    @Override
    public List<PictureAlbum> getList(int categoryId, int limit) {
        PictureAlbum pictureAlbum = new PictureAlbum();
        pictureAlbum.setCategoryId(categoryId);
        PageRequest page = null;
        if (limit > 0) {
            page = new PageRequest(1, limit);
        }
        return getList(pictureAlbum, page);
    }

    @Override
    public int count(PictureAlbum pictureAlbum) {
        return pictureAlbumMapper.count(pictureAlbum);
    }

    @Override
    public int update(PictureAlbum pictureAlbum) {
        if (pictureAlbum.getId() < 1) return 0;
        pictureAlbum.setUpdateTime(LocalDateTime.now());
        return pictureAlbumMapper.update(pictureAlbum);
    }

    @Override
    public int deleteById(int id) {
        if (id < 1) return 0;
        return pictureAlbumMapper.deleteById(id);
    }

    @Override
    public int updateView(int id) {
        if (id < 1) return 0;
        return pictureAlbumMapper.updateView(id);
    }

    @Override
    public List<PictureAlbumVo> getListVo(PictureAlbum pictureAlbum, PageRequest page) {
        List<PictureAlbum> albumList = pictureAlbumMapper.selectList(pictureAlbum, page);
        List<PictureAlbumVo> listVo = new ArrayList<>();
        if(albumList.size() > 0){
            for (PictureAlbum album:albumList) {
                PictureAlbumVo albumVo = new PictureAlbumVo();
                BeanUtils.copyProperties(album,albumVo);
                //展示只设置6条
                albumVo.setPictures(pictureService.getListByAlbumId(album.getId(),new PageRequest(1,6)));
                listVo.add(albumVo);
            }
        }
        return listVo;
    }

    @Override
    public int insertItem(int albumId, int pictureId) {
        if (albumId < 1 || pictureId < 1) {
            return 0;
        }
        return pictureAlbumMapper.insertItem(albumId, pictureId);
    }

    @Override
    public int countItem(int albumId) {
        if(albumId < 1) return 0;
        return pictureAlbumMapper.countItem(albumId);
    }

    @Override
    public int deleteItem(int albumId, int pictureId) {
        return pictureAlbumMapper.deleteItem(albumId, pictureId);
    }

    @Override
    public int deleteItemByAlbumId(int albumId) {
        if(albumId < 1) return 0;
        return pictureAlbumMapper.deleteItemByAlbumId(albumId);
    }

    @Override
    public int deleteItemByPictureId(int pictureId) {
        if(pictureId < 1) return 0;
        return pictureAlbumMapper.deleteItemByPictureId(pictureId);
    }

    @Override
    public List<Integer> getAlbumIdsByPictureId(int pictureId) {
        return pictureAlbumMapper.selectAlbumIdsByPictureId(pictureId);
    }

    @Override
    public PictureAlbum getByPictureId(int id) {
        if(id < 1) return null;
        return pictureAlbumMapper.selectByPictureId(id);
    }
}
